<?php
/***********************************************
 * Name: Interface for UI to commuicate with server
 * File: interface.php
 * Author: PKM
 ***********************************************/
 
require("includes/config.php");
require("includes/function.common.php");

function debug($m){
	echo "DEBUG:\t". $m . "\n";
}

if(!isset($_GET['action'])){
    //value 'action' not set, return;
	echo 'no action';
    return;
}
$action = $_GET['action'];

if (!isset($_GET['pid']))
{
	echo 'no pid';
	return;
}
$playerID = $_GET['pid'];
 
 /* action switching */
if($action == "init"){
	/* Init the game, return the game info and player info to clints */

	$current_state = readState($playerID);
	if ($current_state[0] == 0 && $current_state[1] == 0)
	{
		$num = totalNumOfPlayers();
		if ($num < 3)
		{
			echo 'Not enough players';
		}
		else if ($num > 9)
		{
			echo 'Too much players';
		}
		usleep(1000);
		if (!checkInit()) initialize();
	}
	retrieveState($playerID);
 
}else if($action == "update"){
	 /* Client Request any server updates (waiting period)*/
	 /* this request will be send continuely until server give the 'poll' to client */

	// $current_state = readState($playerID);
	// if ($current_state[0] == 2 && $current_state[1] == 0)
	// {
		// changeAllState(3,0,$current_state[2],$current_state[2]);
		// drawCards($current_state[2], 2);
	// }
	// else if ($current_state[0] == 6 && $current_state[1] == 0)
	// {
		// changeAllState(2, 0, $current_state[2] % totalNumOfPlayers() + 1, $current_state[2] % totalNumOfPlayers() + 1);
	// }
	retrieveState($playerID);
 
}else if($action == "play"){
	/* user play a card */
	/* the card information will be sent in other GET param (to be define)*/
	/* if player skip the turn, it will also send this action with defined param */

	if (!isset($_GET['cid']))
	{
		echo 'No cid';
	}
	if (!isset($_GET['target']))
	{
		echo 'No target';
	}
	if (!isset($_GET['type']))
	{
		echo 'No type';
	}
	
	$cid = (int) $_GET['cid'];
	$target = (int) $_GET['target'];
	$type = (int) $_GET['type'];
	
	// 
	$current_state = readState($playerID);

	
	if ($current_state[3] != $playerID)
	{
		retrieveState($playerID);
		echo "Invalid Play! PID:$playerID != " . $current_state[3];
		return;
	}
	
	if ($current_state[0] == 2 && $current_state[1]) == 0)
	{
		drawCards($current_state[2], 2);
		changeAllState(3,0,$current_state[2],$current_state[2]);
	}
	else if ($current_state[0] == 3 && $current_state[1] == 0)
	{
		// ($cid = 0) => Skip
		if ($cid == 0)
		{
			if (!addToSkipStack($playerID))
				echo 'Cannot add to skip stack!';
			changeAllState(3,0,$current_state[2],$playerID % totalNumOfPlayers() + 1);
			if (isSkipStackFull())
			{
				if (!clearSkipStack())
					echo "Clear not successful.";
				changeAllState(4,0,$current_state[2],$current_state[2]);
			}
		}
		else
		{
			if (!clearSkipStack())
				echo "Clear not successful.";
			
			//	
			// 	playCard Not Yet Finished
			//	
			playCard($playerID, $cid, $target);
			changeAllState(3,0,$current_state[2],$playerID % totalNumOfPlayers() + 1);
		}
	}
	else if ($current_state[0] == 4)
	{
		switch ($current_state[1])
		{
		case 0:
			//if (!checkTransmit())
			if ($playerID == $current_state[3])
			{
				if ($cid == 0 || $type == 1)
				{
					retrieveState($playerID);
					echo 'Invalid move 4.0, either $cid == 0 || $type == 1';
					return;
				}
				// Add checking? Valid Hands?
				// if (!checkCardsInHand($playerID, $cid))
				// {
					// retrieveState($playerID);
					// echo "Invalid move 4.0, Card $cid is not in Hand!";
					// return;
				// }
				
				transmitMessage($playerID, $cid, $target);
				changeAllState(4,1,$current_state[2],$current_state[2]);
				//echo "Message Transmitting";
			}
			else
			{
				retrieveState($playerID);
				echo 'Invalid move 4.0';
				return;
			}
			break;
		case 1:
			//Skip to play cards
			echo 'enter 4.1';
			if ($cid == 0)
			{
				echo "$playerID";
				if (!addToSkipStack($playerID))
					echo 'Cannot add to skip stack!';
				changeAllState(4,1,$current_state[2],$playerID % totalNumOfPlayers() + 1);
				if (isSkipStackFull())
				{
					if (!clearSkipStack())
						echo "Clear not successful.";
					// receiveMessage($playerID, retrieveMessageCID(), $target);
					changeAllState(4,2,$current_state[2],$current_state[2]);
				}
			}
			else
			{
				if (!clearSkipStack())
					echo "Clear not successful.";
				playCard($playerID, $cid, $target);
				changeAllState(4,1,$current_state[2],$playerID % totalNumOfPlayers() + 1);
			}
			break;
		case 2:
			if ($cid == 0)
			{
				if (!addToSkipStack($playerID))
					echo 'Cannot add to skip stack!';
				changeAllState(4,2,$current_state[2],$playerID % totalNumOfPlayers() + 1);
				if (isSkipStackFull())
				{
					if (!clearSkipStack())
						echo "Clear not successful.";
					changeAllState(4,3,$current_state[2],$current_state[2]);
				}
			}
			else
			{
				if (!clearSkipStack())
					echo "Clear not successful.";
				playCard($playerID, $cid, $target);
				changeAllState(4,2,$current_state[2],$playerID % totalNumOfPlayers() + 1);
			}
			break;
		case 3:
			if (confirmMessage($playerID, $cid, $target))
				changeAllState(5,0,$current_state[2],$current_state[2]);
			else
				//changeAllState(4,0,$current_state[2], $target);
				changeAllState(4,0,$current_state[2],$current_state[2]% totalNumOfPlayers()+1);
			break;
		default:
			echo "What happpens?";
		}
	}
	else if ($current_state[0] == 5)
	{
		// ($cid = 0) => Skip
		if ($cid == 0)
		{
			if (!addToSkipStack($playerID))
				echo 'Cannot add to skip stack!';
			changeAllState(5,0,$playerID,$playerID % totalNumOfPlayers() + 1);
			if (isSkipStackFull())
			{
				if (!clearSkipStack())
					echo "Clear not successful.";
				changeAllState(6,0,nextPlayer($current_state[2], 1),nextPlayer($current_state[2], 1));
			}
		}
		else
		{
			if (!clearSkipStack())
				echo "Clear not successful.";
			playCard($playerID, $cid, $target);
			changeAllState(5,0,$playerID,$playerID % 7 + 1);
		}
	}

	retrieveState($playerID);
	
}else if($action == ""){
 
 
 
}
 



?>